$("#addEnvBtn").click(function () {
    console.log($("#env_add_form").serialize());
    $.ajax({
        url: "/flytest/env/environment",
        type: "post",
        data: $("#env_add_form").serialize(),
        success: function (result) {
            console.log(result);
            if (result.code === '200') {
                window.location.href = "/flytest/env/"
            }
        }
    });
});


function setEnvUpdateModalValue(envId) {
    $.ajax({
        url: "/flytest/env/environment?envId=" + envId,
        type: "get",
        async: false,
        dataType: "json",
        success: function (result) {
            console.log(result.env);
            $("#envId_u").val(result.env.envId);
            $("#envName_u").val(result.env.envName);
            $("#hostAddress_u").val(result.env.hostAddress);
            $("#hostDescription_u").val(result.env.hostDescription);
            if (result.env.isOpened === false) {
                console.log("open");
                $("#isOpened_u").val("0");
            } else {
                $("#isOpened_u").val("1");
            }

        }
    });
}

$("#updateEnvBtn").click(function () {
    $.ajax({
        url: "/flytest/env/environment",
        type: "put",
        data: $("#env_update_form").serialize(),
        dataType: "json",
        success: function (result) {
            console.log(result);
            if (result.code === '200') {
                window.location.href = "/flytest/env/"
            } else {
                swal("未知错误请联系管理员");
            }
        }
    });
});

function deleteEnvBtnClicked(envId) {
    bootbox.confirm({
        message: "确认删除当前测试环境吗？",
        buttons: {
            confirm: {
                label: 'Yes',
                className: 'btn-success'
            },
            cancel: {
                label: 'No',
                className: 'btn-danger'
            }
        },
        callback: function (result) {
            console.log("will delete the env id : " + envId);
            if (result) {
                $.ajax({
                    url: "/flytest/env/environment?envId=" + envId,
                    type: "delete",
                    dataType: "json",
                    success: function (result) {
                        if (result.code === "200") {
                            window.location.href = "/flytest/env/"
                        } else {
                            swal("删除失败，请联系管理员")
                        }
                    }
                })
            }
        }
    });
}